package ${namespace}.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import ${namespace}.entity.${bclazz}Entity;
import ${namespace}.service.${bclazz}Service;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * ${comment}web控制层
 * @author ${author}
 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
 */
@RequestMapping("/${sclazz}")
@Controller
public class ${bclazz}Controller extends BaseController {

	private Logger logger = LoggerFactory.getLogger(${bclazz}Controller.class);

	@Autowired
	private ${bclazz}Service ${sclazz}Service;
	
	/**
	 * 跳转${comment}查询页面
	 * @param ${bclazz} 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author ${author}
 	 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
	 */
	@RequestMapping("/toList${bclazz}Page")
	public String toList${bclazz}Page(${clazz} ${sclazz}, HttpServletRequest request) {
		return "${module}/${sclazz}_list";
	}

	/**
	 * 分页查询${comment}列表
	 * @param ${sclazz} 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author ${author}
 	 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
	 */
	@RequestMapping("/list${bclazz}Page")
	@ResponseBody
	public JSONGrid list${bclazz}Page(${clazz} ${sclazz}, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			PageBean pageBean = ${sclazz}Service.list${bclazz}Page(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询${comment}失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author ${author}
 	 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
	 */
	@RequestMapping("/toAdd${bclazz}")
	public String toAdd${bclazz}(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "${module}/${sclazz}_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author ${author}
 	 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
	 */
	@RequestMapping("/toUpdate${bclazz}")
	public String toUpdate${bclazz}(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			${bclazz}Entity ${sclazz} = ${sclazz}Service.get${bclazz}ById(id);
			request.setAttribute("${sclazz}", ${sclazz});
		} catch (Exception e) {
			handleException("跳转编辑${comment}页面错误", logger, e);
		}
		return "${module}/${sclazz}_form";
	}


	/**
	 * 新增或保存${comment}
	 * @param request 请求对象
	 * @param ${sclazz}Entity ${comment}实体
	 * @return
	 * @author ${author}
 	 * @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
	 */
	@RequestMapping("/saveOrUpdate${bclazz}")
	@ResponseBody
	public Map<String, Object> save${bclazz}(HttpServletRequest request, ${bclazz}Entity ${sclazz}Entity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			<#list collist as col>
			<#if col.defaultVal?? && col.defaultVal != 'NG'>
			${sclazz}Entity.set${col.bpropertyName}(${col.controllerDefault});
			</#if>
			</#list>
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = ${sclazz}Service.update${bclazz}(${sclazz}Entity);
			}else{
				result = (int)${sclazz}Service.save${bclazz}(${sclazz}Entity);
			}
			resultMap.put("code", result);
		} catch (Exception e) {
			handleException("添加${comment}失败", logger, e);
		}
		return resultMap;
	}
	
}
